let fs = require('fs');
let router = require('koa-router');
let routes = require('../controllers')


function search() {
    let controllersDir = '/controllers' || '';
    let files = fs.readdirSync(controllersDir);

    let endFiles = files.filter((fileName)=>{
        return fileName.endsWith('.js') && fileName !== 'default.js'
    })

    files.forEach(item => {
        if (item.endsWith('.js')) {
            let routerObj = require(path.join(controllersDir, item))
            console.log(routerObj);
            for (var r in routerObj) {
                let type = routerObj[r][0]
                let content = routerObj[r][1]
                if (type === 'get') {
                    router.get(r, content);
                } else {
                    router.post(r, content);
                }
            }
        }
    })
    return router.routes();
}


module.exports = search;